package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

/**
* @Description: 接入码有效期属性处理类
*
* @author lyx
* @date 2020年12月18日
* @version V1.0
* @Package com.iwhalecloud.bss.kite.cucc.service.attr.handler
*/
public class AccessEffDateAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;
    
    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        // 变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(
                querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))
            || KiteStringUtils.equals(attr.getOldValue(), value)) {
            return;
        }

        if (KiteStringUtils.isNotEmpty(value)) {
            int rcs5GDate = Integer.parseInt(value.replace("-", ""));
            DateFormat df = new SimpleDateFormat("yyyyMMdd");
            Calendar calendar = Calendar.getInstance();
            int nowdate = Integer.parseInt(df.format(calendar.getTime()));
            if (rcs5GDate <= nowdate) {
                ErrorConsts.BUSI_COMMON_ERROR.throwOut("接入码有效期需要大于当前时间，请重新输入！");
            }
        }
    }
}
